package com.novanok.mailtweets;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.novanok.mailtweets.adapter.ListaCarpetaAdapter;
import com.novanok.mailtweets.adapter.ListaCuentaAdapter;
import com.novanok.mailtweets.entidad.Carpeta;

import java.util.ArrayList;
import java.util.Map;

public class ActivityCarpeta extends Activity {

    private static final String TAG = "ActivityCarpeta";
    public static Carpeta carpetaSeleccionada = null;

    private Map<String, Carpeta> carpetas = null;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView(R.layout.activity_carpeta);

        this.carpetas = MyActivity.getCarpetas();

        setClickLista();
    }

    public void onResume(){
        super.onResume();
        this.carpetas = MyActivity.getCarpetas();
        this.actualizar();
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu ) {
        getMenuInflater().inflate( R.menu.menu_cuenta, menu );
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch ( item.getItemId() ) {
            case R.id.nuevo_cuenta:
                this.nuevaCarpeta();
                return true;

            case R.id.eliminar_cuenta:
                this.eliminarCarpeta();
                return true;

            case R.id.volver_cuenta:
                finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void actualizar(){
        if ( this.carpetas != null ) {
            ArrayList<Carpeta> listaCarpeta = new ArrayList<Carpeta>();

            for ( Carpeta c : this.carpetas.values() ){
                listaCarpeta.add(c);
            }

            (( ListView ) ActivityCarpeta.this.findViewById( R.id.lstCarpetas )).setAdapter(
                    new ListaCarpetaAdapter( ActivityCarpeta.this, listaCarpeta ) );
        }
    }

    public void eliminarCarpeta (){
        if ( carpetaSeleccionada != null ) {
            this.carpetas.remove( String.valueOf( carpetaSeleccionada.getId() ) );
            this.actualizar();

            Log.d(TAG, "Eliminacion de la Carpeta");
        }
    }

    public void nuevaCarpeta(){
        Intent i = new Intent( this, ActivityCarpetaItem.class );
        i.putExtra("carpeta_accion", "nuevo");
        startActivity(i);
    }

    private void setClickLista(){
        ListView lv = ( ListView )findViewById( R.id.lstCarpetas );

        lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick( AdapterView<?> adapterView, View view, int i, long l ) {
                view.setSelected( true );

                ListaCarpetaAdapter adapter = ( ListaCarpetaAdapter )( ( ListView ) findViewById( R.id.lstCarpetas ) ).getAdapter();
                Carpeta toRemove = adapter.getItem( ( ( ListView ) findViewById( R.id.lstCarpetas ) ).getPositionForView( view ) );
                ActivityCarpeta.carpetaSeleccionada = toRemove;

                Log.d( TAG, "Click Item Lista " + toRemove.getDescripcion() );
            }
        });
    }
}
